/**
 * Created by Administrator on 2017/2/11.
 */
import { Component,OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';

import { ResService }  from './resdetail.serve'
declare let $:any;
@Component({
    moduleId: module.id,
    selector: 'my-resdetail',
    templateUrl: 'resdetail.html',
  providers:[ ResService ]
})
export class ResfactoryComponent implements OnInit{
  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private resService:ResService,
  ){}
    private detailid='';
    private detailinfro='';
    private errorMessage:string;
  private collectinfro='';
  private succ='';
   ngOnInit(){
      this.route.params
        .subscribe(
        (params:Params)=>{this.detailid= params['id'];}
        );
      let giveid = this.detailid;
      console.log(giveid);
     this.resService.resdet(giveid)
       .subscribe(
         res => {this.detailinfro = res['data'];
           console.log(this.detailinfro)
         },
         error =>this.errorMessage = <any>error
       )
     $(function() {
       //选择收藏夹
       $('#collectModal').on('click', '._list>li', function() {
         $(this).parent().find('li').removeClass('active');
         $(this).addClass('active');
         $('#collectModal').modal('hide');
       });

       //点击右边小图显示在大图位置
       $('._res-right').on('click', '._res-smallimg>li._res-img>img', function() {
         $('#bigImg').attr('src', $(this).attr('src'));
       });

       var index = 0;
       //右边小图片-上一页
       $('._res-right').on('click', '._res-smallimg>li._res-arrow-up', function() {
         if($(this).hasClass('disabled')) {
           return;
         }
         var count = $('._res-smallimg>li._res-img').length;
         var top = 0;
         index--;
         if(-(index * 108) <= top) {
           $('._res-smallimg>li._res-img').css('top', '-' + 108 * index + 'px');
           $('._res-smallimg>li._res-arrow-down').removeClass('disabled');
         }
         if(index * 108 == 0) {
           $(this).addClass('disabled');
         }
       });
       //右边小图片-下一页
       $('._res-right').on('click', '._res-smallimg>li._res-arrow-down', function() {
         if($(this).hasClass('disabled')) {
           return;
         }
         var count = $('._res-smallimg>li._res-img').length;
         var top = (count - 3) * 108;
         index++;
         if(index * 108 <= top) {
           $('._res-smallimg>li._res-img').css('top', '-' + 108 * index + 'px');
           $('._res-smallimg>li._res-arrow-up').removeClass('disabled');
         }
         if(index * 108 == top) {
           $(this).addClass('disabled');
         }
       });
     });
   }
  colltlist(){
    this.resService.collectlist()
      .subscribe(
        res => {this.collectinfro = res;
          console.log(this.collectinfro)
        },
        error =>  this.errorMessage = <any>error);
  }
  addcoll(collect_id:string,product_id:string){
    this.resService.addcollect(collect_id,product_id)
      .subscribe(
        res=>{
          this.succ=res
        }
      )
  }
}
